package br.mg.bhe.ftpobre.fs;

import org.apache.commons.net.ftp.FTPFile;

/**
 * 
 * @author Aureliano
 */
public class RemoteFile {

	private static RemoteFile upFolder;
	
	private FTPFile file;
	
	public RemoteFile() {
		super();
	}
	
	public static RemoteFile getUpFolder() {
		if (upFolder != null) {
			return upFolder;
		}
		
		upFolder = new RemoteFile();
		FTPFile f = new FTPFile();
		f.setName("/..");
		f.setSize((long) -1);
		f.setType(FTPFile.DIRECTORY_TYPE);
		upFolder.setFile(f);
		
		return upFolder;
	}
	
	public FTPFile getFile() {
		return file;
	}
	
	public void setFile(FTPFile file) {
		this.file = file;
	}
	
	public boolean isDirectory() {
		return (this.file != null) ? this.file.isDirectory() : false;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((file.getName() == null) ? 0 : file.getName().hashCode());
		result = prime * result + ((file.isDirectory()) ? 1231 : 1237);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RemoteFile other = (RemoteFile) obj;
		if (file.getName() == null) {
			if (other.file.getName() != null)
				return false;
		} else if (!file.getName().equals(other.file.getName()))
			return false;
		if (file.getSize() != other.file.getSize())
			return false;
		if (file.getType() != other.file.getType())
			return false;
		return true;
	}

	@Override
	public String toString() {
		return (this.file != null) ? this.file.getName() : "";
	}
}